<template>
  <ul class="list">
      <li class="item" v-for="item of letters" :key='item' :ref='item'
      @click='handleLetterClick'
      @touchstart='start'
      @touchend='end'
      @touchmove='move'>
        {{item}}
      </li>
  </ul>
</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    letters: Array
  },
  data () {
    return {
      status: false,
      startY: 74,
      timer: null
    }
  },
  methods: {
    handleLetterClick (e) {
      const letter = e.target.innerText
      this.$emit('change', letter)
    },
    start () {
      this.status = true
    },
    end () {
      this.status = false
    },
    // updated () {
    //   this.startY = this.$refs['A'][0].clientY
    // },
    move (e) {
      if (this.status) {
        if (this.timer) {
          clearTimeout(this.timer)
        }
        this.timer = setTimeout(() => {
          const index = Math.floor((e.touches[0].clientY - this.startY - 79) / 20)
          console.log(e.touches[0].clientY)
          if (index >= 0 && index < this.letters.length) {
            const letter = this.letters[index]
            this.$emit('change', letter)
          }
        }, 8)
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';
.list
  width .4rem
  position absolute
  top 1.58rem
  right .05rem
  bottom 0
  display flex
  flex-direction column
  justify-content center
  .item
    line-height .4rem
    color $bgColor
    text-align center
</style>
